package com.kun.controller.admin;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.Context;

import com.kun.entity.Recommended;
import com.kun.service.ActivityService;
import com.kun.service.BookService;
import com.kun.service.RecommentService;
import com.kun.service.UserService;
import com.kun.util.StaticPageGenerateUtil;

/**
 * @description: 静态化功能
 * @author: KM
 * 2018年5月9日 下午4:32:37
 */
@Controller
@RequiresAuthentication
@RequestMapping("/admin")
public class GenerateAdminController {
	
	@Autowired
	private BookService  bookServiceImpl;
	
	@Autowired
	private UserService  userServiceImpl;
	
	@Autowired
	private RecommentService recommentService;
	
	@Autowired
	private ActivityService activityService;
	
	
	//跳转静态化页面 
	@RequiresPermissions("pageGenerate:view")
	@RequestMapping("/generate")
	public String generate(){
		return "admin/generate";
	}
	
	/**
	 * 首页静态化轮播
	 * */
	@RequiresPermissions("circlePictureState")
	@RequestMapping("/generateRecommended")
	@ResponseBody
	public String generateRecommended(){
		List<Recommended> recommendedList = recommentService.selectRecommendedforIndex(5);
		Context context = new Context();
		context.setVariable("recommendedList", recommendedList);
		String msg = StaticPageGenerateUtil.generatePage("generation/recommended", "recommended", context);
		return msg;
	}
	
	/**
	 * 首页静态化活动
	 * */
	@RequiresPermissions("activitySate")
	@RequestMapping("/generateActivity")
	@ResponseBody
	public String generateActivity(){
		String jsonString = "{issueTime:'desc'}";
		Map<String, Object> map  = activityService.selectActivityTiltle(1, 5, jsonString, false);
		Context context = new Context();
		context.setVariable("activityList", map.get("activityList"));
		String msg = StaticPageGenerateUtil.generatePage("generation/activity", "activity", context);
		return msg;
	}
	
	/**
	 * 首页静态化共享排行
	 * */
	@RequiresPermissions("shareState")
	@RequestMapping("/generateShareRank")
	@ResponseBody
	public String generateShareRank(){
		String jsonString = "{level:'desc'}";
		Map<String, Object> map  = userServiceImpl.selectAllUser(1, 5, jsonString, false);
		Context context = new Context();
		context.setVariable("userList", map.get("userList"));
		String msg = StaticPageGenerateUtil.generatePage("generation/shareRank", "shareRank", context);
		return msg;
	}
	
	/**
	 * 首页静态化借阅排行
	 * */
	@RequiresPermissions("borrowState")
	@RequestMapping("/generateBorrowRank")
	@ResponseBody
	public String generateBorrowRank(){
		String jsonString = "{borrow:'desc'}";
		Map<String, Object> map  = bookServiceImpl.selectAllBook(1, 5, jsonString, false);
		Context context = new Context();
		context.setVariable("bookList", map.get("bookList"));
		String msg = StaticPageGenerateUtil.generatePage("generation/borrowRank", "borrowRank", context);
		return msg;
	}

}
